package pl.jtb.ctl.tabory.ui.boe.ui.ce;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.Section;

import pl.jtb.ctl.tabory.dao.api.services.ICompositionService;
import pl.jtb.ctl.tabory.dao.api.services.IVagonService;
import pl.jtb.ctl.tabory.dao.data.vo.CompositionVO;
import pl.jtb.ctl.tabory.dao.data.vo.VagonVO;
import pl.jtb.ctl.tabory.dao.impl.mybatis.servicemanager.ServiceManager;
import pl.jtb.ctl.tabory.ui.boe.osgi.Activator;
import pl.jtb.ctl.tabory.ui.boe.selection.CompositionSelection;
import pl.jtb.ctl.tabory.ui.editors.AbstractMasterDetailSection;
import pl.jtb.ctl.tabory.ui.editors.EditorStateSL;
import pl.jtb.ctl.tabory.ui.selection.ISelectionListener;

public class CompositionEditor extends AbstractMasterDetailSection<CompositionSelection, CompositionMasterSectionPart, CompositionDetailSectionPart> {

    public static final String ID = "pl.jtb.ctl.tabory.ui.boe.ui.ce.CompositionEditor";
    private final static Logger logger = Logger.getLogger(CompositionEditor.class);

    public CompositionEditor() throws Exception {
        super();
    }

    @Override
    protected CompositionMasterSectionPart getMasterSection(Form parentForm) {
        CompositionMasterSectionPart sectionPart = new CompositionMasterSectionPart(parentForm.getBody(), formToolkit, Section.TITLE_BAR, Activator.getMessage("compositions.master.title"), true, true);
        sectionPart.setSelection(selection);
        return sectionPart;
    }

    @Override
    protected CompositionDetailSectionPart getDetailSection(Form parentForm) {
        CompositionDetailSectionPart sectionPart = new CompositionDetailSectionPart(parentForm.getBody(), formToolkit, Section.TITLE_BAR, Activator.getMessage("compositions.details.title"));
        sectionPart.setSelection(selection);
        return sectionPart;
    }

    @Override
    protected void postInitGui(Composite parent) {
        masterSectionPart.addSelectionListener(new ISelectionListener<CompositionSelection>() {
            @Override
            public void selectionChanged(final CompositionSelection selection) {
                logger.debug("Selection changed");
                BusyIndicator.showWhile(Display.getDefault(), new Runnable() {
                    @Override
                    public void run() {
                        EditorStateSL editorStateSL = selection.getEditorState();
                        if (editorStateSL.equals(EditorStateSL.CLEAN)) {
                            setDirty(false);
                        } else {
                            setDirty(true);
                        }
                        masterSectionPart.setSelection(selection);
                        detailSectionPart.setSelection(selection);
                    }
                });
            }
        });
    }

    @Override
    protected void doSave() throws Exception {
        ICompositionService compositionService = ServiceManager.getInstance().getCompositionService();
        CompositionSelection selection = detailSectionPart.getSelection();
        CompositionVO selectedComposition = selection.getSelectedComposition();
        if (selectedComposition.getId() == null) {
            CompositionVO composition = compositionService.getComposition(selectedComposition.getInternalId());
            if (composition != null) {
                MessageDialog.openError(getSite().getShell(), Activator.getMessage("compositions.validation.save.notunique.title"), Activator.getMessage("compositions.validation.save.notunique.message"));
                return;
            }
        }
        compositionService.saveUpdateComposition(selection.getSelectedComposition());
        compositionService.saveUpdateCompositionVagonRelation(selection.getMapCompositionVagonRelation());
        selection = initSelection();
        masterSectionPart.setSelection(selection);
        detailSectionPart.setSelection(selection);
        setDirty(false);
    }

    @Override
    protected void doCancel() throws Exception {
        selection = initSelection();
        masterSectionPart.setSelection(selection);
        detailSectionPart.setSelection(selection);
        setDirty(false);
    }

    @Override
    protected CompositionSelection initSelection() throws Exception {
        logger.debug("Creating selection");
        ICompositionService compositionService = ServiceManager.getInstance().getCompositionService();
        IVagonService vagonService = ServiceManager.getInstance().getVagonService();
        List<VagonVO> listAllVagons = vagonService.getListAllVagons();
        CompositionSelection selection = new CompositionSelection();
        selection.setEditorState(EditorStateSL.CLEAN);
        selection.setListAllVagons(listAllVagons);
        List<CompositionVO> listAllCompositions = compositionService.getListAllCompositions();
        selection.setListAllCompositions(listAllCompositions);
        Map<CompositionVO, List<VagonVO>> mapRelation = new LinkedHashMap<CompositionVO, List<VagonVO>>();
        for (CompositionVO composition : listAllCompositions) {
            mapRelation.putAll(compositionService.getCompositionVagonRelation(composition));
        }
        selection.setMapCompositionVagonRelation(mapRelation);
        logger.debug("Selection created: " + selection);
        return selection;
    }

    @Override
    public void setFocus() {
        setDirty(true);
        editorCancelAction.run();
    }

}
